// 簿记管理页面与簿记交收页面用的查询条件组件
import React from 'react';
import { DATA_ENTRY, AdvancedSearch, TradingClassif, codeType } from '../../../comps';
import moment from 'moment';
class QueryC extends React.Component {
  state = {
    businessType: -1, // 当前选中的业务类型
    directionDisable: true, // 交易方向是否可选
    businessTypeData: undefined,
    businessTypeChild: undefined,
  };

  componentDidMount() {
    this.newTradingClassif = TradingClassif.filter(
      (item) => item.value === '0' || item.value === '1' || item.value === '6' || item.value === '12' || item.value === '21' || item.value === '20',
    );
  }
  // 响应业务类型变更
  businessTypeChanged = (e) => {
    if (e) {
      if (Array.isArray(e)) {
        this.setState({ businessType: -1, directionDisable: true, businessTypeData: e });
      } else {
        this.setState({ businessType: e, directionDisable: false, businessTypeData: undefined });
      }
    } else {
      this.setState({ businessType: -1, directionDisable: true, businessTypeData: undefined });
    }
  };

  // 响应查询
  onQuery = (query) => {
    // 证券代码组件会带出exchangeCode，本接口不需要，删除
    delete query.exchangeCode;

    // 日期格式转换
    query.search_GTE_createdTs = query.search_GTE_createdTs ? query.search_GTE_createdTs.format('YYYY-MM-DD') : '';
    query.search_LTE_createdTs = query.search_LTE_createdTs ? query.search_LTE_createdTs.format('YYYY-MM-DD') : '';
    if (query.search_EQ_type) {
      if (Array.isArray(this.state.businessTypeData)) {
        query.search_IN_type = this.state.businessTypeData.join(';');
        delete query.search_EQ_type;
      }
    }
    let queryItems = {
      ...query,
      search_IN_method: '0;1', // 在簿记页面，后台要求加，与指令区分
      userId: localStorage.getItem('antd-pro-userId'),
    };

    // 抛出查询事件
    this.props.onQuery(queryItems);
  };

  // 响应重置
  onClear = () => {
    // 抛出重置事件
    this.props.onClear();
    this.setState({ businessType: -1, directionDisable: true });
    this.state.businessTypeChild.clear();
  };

  tradingCodeChange = (e) => {
    if (e) {
      this.state.tradingCodeSelect.setSelect(e.exchangeCode);
      this.setState({
        securityExchangeData: e.exchangeCode,
      });
    }
  };
  render() {
    return (
      <AdvancedSearch
        dataSource={[
          {
            dataIndex: ['search_GTE_createdTs', 'search_LTE_createdTs'],
            title: '日期区间',
            type: DATA_ENTRY.RANGEDATE,
            initialValue: [moment().subtract(1, 'months'), moment()],
          },
          {
            dataIndex: 'portfolioId',
            title: '产品组合',
            multiple: true,
            type: DATA_ENTRY.PRODUCTGROUPTREE,
            tradeModel: ['BOOKKEEPING_MODE', 'COMMAND_MODE'], //不包含估值解析和估值对接的产品
          },
          {
            dataIndex: 'search_EQ_type', //业务类型
            type: DATA_ENTRY.BUSINESSTYPE,
            appType: '3',
            onChange: (e) => this.businessTypeChanged(e),
            onRef: (ref) => {
              this.state.businessTypeChild = ref;
            },
          },
          {
            dataIndex: 'search_LIKE_direction',
            title: '交易方向',
            businessType: this.state.businessType,
            type: DATA_ENTRY.TRADINGDIRECT,
            disabled: this.state.directionDisable,
            appType: '3',
          },
          {
            dataIndex: 'search_LIKE_stockCode',
            title: '证券代码',
            type: DATA_ENTRY.SECURITIESCODE,
            codeType: codeType.positioned,
            queryItems: { securityType: '0,7,12' },
            onChange: (e) => this.tradingCodeChange(e),
          },
          {
            dataIndex: 'search_EQ_tradingMarket',
            title: '交易市场',
            type: DATA_ENTRY.TRADINGMARKET,
            marketType: ['jys', 'yhj', 'cw'],
            onRef: (ref) => {
              this.state.tradingCodeSelect = ref;
            },
          },
        ]}
        onQuery={this.onQuery}
        onClear={this.onClear}
      />
    );
  }
}

export default QueryC;
